//
//  JSWULIUController.swift
//  fho
//
//  Created by 郭颢源 on 2021/5/18.
//

import UIKit

extension JSWULIUController: UITableViewDelegate,UITableViewDataSource {
    
    // 区的数量
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    // 区内的元素数量
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return dataArray.count
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 110
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(for: JSWULIUViewCell.self)

        let model = dataArray[indexPath.row] as! JSWULListModel
//        http://static.56114.net.cn/gongsi/20180425/1524645857.jpg
//        cell.imgView.kf.indicatorType = .activity
        let imageUrl = "http://static.56114.net.cn/" + model.picture

        cell.imgView.kf.setImage(with:URL(string: imageUrl), placeholder: UIImage(named: "default"))
        
        cell.title.text = model.name
        cell.content.text = "地址：" + model.addressshow!
        cell.Telephone = model.Telephone
        return cell
    }
    
}


class JSWULIUController: UIViewController {

    private lazy var cellId = "cellId"
    fileprivate lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)//注册cell
        
        tableView.showsVerticalScrollIndicator = false//垂直滚动指示器
        tableView.showsHorizontalScrollIndicator = false//水平滚动指示器
        tableView.delegate = self
        tableView.dataSource = self

        tableView.separatorStyle = .none//分割线
        tableView.estimatedRowHeight = 0//预设行高
        tableView.estimatedSectionFooterHeight = 0//预设分区头高度
        tableView.estimatedSectionHeaderHeight = 0
        tableView.backgroundColor = UIColor.white
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
        }
        return tableView
    }()
    var dataArray = [Any]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = .init()
        self.title = "查物流"
        // Do any additional setup after loading the view.
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        let params = [
//        ?kw=%E9%83%91%E5%B7%9E&p=1
            "kw":"郑州",
            "p":"2"
        ]
        
        HttpRequest.request(.get, "https://zz.56114.com/wx/company/search", params) { (result) in
            print(result)
            if let resp = JSONDeserializer<JSWULModel>.deserializeFrom(json: result.rawString())
            {
                self.dataArray.append(contentsOf: resp.list!)
                self.tableView.reloadData()
            }
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
